﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LDTB.Service;
using System.Globalization;

namespace LDTB
{
    public partial class FormServeyAdd : Form
    {

        LDTBDataDataContext DB = DataService.DB;
        public int userid;

        public FormServeyAdd()
        {
            InitializeComponent();

        }

        public FormServeyAdd(int id_)
        {

            InitializeComponent();
            userid = id_;
        }

        private void FormServey_Load(object sender, EventArgs e)
        {

            //

            //load cap so

            List<ItemExtra> ltCompa = new List<ItemExtra>();
            for (int i = 1; i < 100; i++)
            {
                ltCompa.Add(new ItemExtra { ID = i, Name = i < 10 ? "0" + i.ToString() : i.ToString() });
            }
            ddlCompareNumber.DataSource = ltCompa;
            ddlCompareNumber.DisplayMember = "Name";
            ddlCompareNumber.ValueMember = "ID";


            //khoi tao loai doi tuong


            List<ItemExtra> ltSex = new List<ItemExtra>();

            ltSex.Add(new ItemExtra { ID = 0, Name = "Nam" });
            ltSex.Add(new ItemExtra { ID = 1, Name = "Nữ" });
            this.ddlSex.DataSource = ltSex;
            this.ddlSex.DisplayMember = "Name";
            this.ddlSex.ValueMember = "ID";
            this.ddlSex.SelectedValue = 0;

            var ward = from w in DB.Wards
                       select w;
            this.ddlWard.DataSource = ward;
            this.ddlWard.ValueMember = "ID";
            this.ddlWard.DisplayMember = "WardName";


            //khoi tao lang xa
            int wardid = (int)this.ddlWard.SelectedValue;

            var village = from v in DB.Villages
                          where v.WardID == wardid
                          select v;
            this.ddlVillage.DataSource = village;
            this.ddlVillage.ValueMember = "VillageID";
            this.ddlVillage.DisplayMember = "VillageName";

            //khoi tao lua chon cho doi tuong

            var catob = from c in DB.CateObjects
                        select c;
            this.ddlCatObject.DataSource = catob;
            this.ddlCatObject.DisplayMember = "CatName";
            this.ddlCatObject.ValueMember = "CatID";

            //khoi tao gia tri
            int id = (int)ddlCatObject.SelectedValue;
            var catopt = from c in DB.OptionCates
                         where c.CateDisableID == id
                         select c;

            this.ddlCatOp.DataSource = catopt;
            this.ddlCatOp.DisplayMember = "DegreeName";
            this.ddlCatOp.ValueMember = "ID";



        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            //khoi tao gia tri
            int id;
            if (ddlCatObject.SelectedValue.GetType().Name == "CateObject")
            {
                id = (ddlCatObject.SelectedValue as CateObject).CatID;
            }
            else
            {
                id = (int)ddlCatObject.SelectedValue;
            }

            var catopt = from c in DB.OptionCates
                         where c.CateDisableID == id
                         select c;

            this.ddlCatOp.DataSource = catopt;
            this.ddlCatOp.DisplayMember = "DegreeName";
            this.ddlCatOp.ValueMember = "ID";
        }

        public void CreateCoboForOptionCat(int catid)
        {

            /**
             * Khoi tao Option Cat 
             * Luy y : co gia tri mac dinh cho option voi gia tri mac dinh "---Chon --"
            */


        }

        public void CreateCoBoForCatObject()
        {
            /**
             * Khoi tao Option Cat 
             * Luy y : co gia tri mac dinh cho option voi gia tri mac dinh "---Chon --"
            */

        }

        private void SavePeople()
        {


            int yearbirth;
            DateTime startDate;
            CultureInfo ci = new CultureInfo("en-IE");

            if (this.txtFaminiName.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Nhập họ ");
                txtFaminiName.Focus();

            }
            else if (this.txtFirstName.Text.Trim() == string.Empty)
            {

                MessageBox.Show("Nhập tên");
                this.txtFirstName.Focus();

            }
            else if (this.ddlSex.SelectedIndex == -1)
            {

                MessageBox.Show("Chọn giới tính");
            }
            else if (this.txtYearBirthday.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Nhập năm sinh");
                this.txtYearBirthday.Focus();
            }
            else if (this.txtMoneyMonth.Text.Trim() == string.Empty)
            {

                MessageBox.Show("Nhập số tiền nhận hàng tháng");
                this.txtMoneyMonth.Focus();

            }
            else if (this.txtMoneyStartDate.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Nhập ngày thời điểm nhận lương ");
                this.txtMoneyStartDate.Focus();
            }
            else if (!DateTime.TryParseExact(this.txtMoneyStartDate.Text, "MM/yyyy", ci, DateTimeStyles.None, out startDate))
            {
                MessageBox.Show("Ngày tháng không hợp lệ tháng/năm");
                this.txtMoneyStartDate.Focus();

            }
            else if (!int.TryParse(txtYearBirthday.Text, out yearbirth))
            {

                MessageBox.Show("Năm sinh phải ở dạng số");
                this.txtYearBirthday.Focus();

            }
          
            else if (yearbirth > 2050)
            {
                MessageBox.Show("Năm sinh nhỏ hơn 2050");
                this.txtYearBirthday.Focus();
            }
            else
            {

                bool sex = (int)ddlSex.SelectedValue == 0 ? true : false;
                //CultureInfo provider = CultureInfo.InvariantCulture;
                //DateTime startDate = DateTime.ParseExact(txtMoneyStartDate.Text, "MM/yyyy", provider);

                var peope = new People
                {

                    FirstName = txtFirstName.Text.Trim(),
                    FamilyName = txtFaminiName.Text.Trim(),
                    BirdthDay = int.Parse(txtYearBirthday.Text),
                    CatID = (int)ddlCatObject.SelectedValue,
                    DegreeID = (int)ddlCatOp.SelectedValue,
                    LocationID = (int)ddlVillage.SelectedValue,
                    NumberPair = byte.Parse(ddlCompareNumber.SelectedValue.ToString()),
                    Sex = sex,
                    IsPoor = chkIsPoor.Checked,
                    Address = txtAddress.Text.Trim(),
                    ReciveMoneyMonth = int.Parse(txtMoneyMonth.Text),
                    ReciveMoneyStartDate = startDate,
                    UserID = userid

                };


                try
                {

                    DB.Peoples.InsertOnSubmit(peope);
                    DB.SubmitChanges();

                    if (peope.ID != 0)
                    {
                        MessageBox.Show("Thêm  thành công");
                        txtFirstName.Text = "";
                        this.txtFaminiName.Text = "";
                        txtAddress.Text = "";
                        this.txtFaminiName.Focus();
                    }
                    else
                    {

                        MessageBox.Show("Chưa thành công");

                    }

                }
                catch (Exception e_)
                {

                    MessageBox.Show(e_.Message);
                }
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            SavePeople();
        }

        private void maskedTextBox1_Validating(object sender, CancelEventArgs e)
        {
            DateTime rs;
            CultureInfo ci = new CultureInfo("en-IE");
            if (!DateTime.TryParseExact(this.txtMoneyStartDate.Text, "MM/yyyy", ci, DateTimeStyles.None, out rs))
            {
                e.Cancel = true;
                MessageBox.Show("Ngày tháng không hợp lệ tháng/năm");
            }
        }

        private void maskedTextBox2_Validating(object sender, CancelEventArgs e)
        {
            // kiem tra gia trị tiền
        }


        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ddlWard_SelectionChangeCommitted(object sender, EventArgs e)
        {

            //khoi tao lang xa
            int wardid = (int)this.ddlWard.SelectedValue;

            var village = from v in DB.Villages
                          where v.WardID == wardid
                          select v;
            this.ddlVillage.DataSource = village;
            this.ddlVillage.ValueMember = "VillageID";
            this.ddlVillage.DisplayMember = "VillageName";

        }

        private void button2_Click(object sender, EventArgs e)
        {
            SavePeople();
            Close();
        }

    }
}
